/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;

// Log4net
using log4net;
using log4net.Config;

namespace Engine
{
    public enum LogType
    {
        DEBUG,
        ERROR,
        FATAL,
        INFORMATION,
        WARNING
    };

    /// <summary>
    ///     Logger, stores messages in the configured file.
    /// 
    ///     Dependancy: Log4net
    /// </summary>
    public class Logger
    {
        # region Attribute

        private ILog log;

        # endregion


        # region Constructor

        /// <summary>
        ///     Constructor using string object.
        /// </summary>
        /// 
        /// <param name="name">Identifier of logger</param>
        public Logger(string name)
        {
            // Retrieve logger
            log = LogManager.GetLogger(name);
        }

        # endregion


        # region Function

        /// <summary>
        ///     Configure logger, using a configuration file.
        /// </summary>
        /// 
        /// <param name="file">Configuration path</param>
        public static bool Configure(string file)
        {
            try
            {
                XmlConfigurator.Configure(new System.IO.FileInfo(file));

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///     Log a certain message, with a certain type.
        /// </summary>
        /// 
        /// <param name="message">Message</param>
        /// <param name="type">Type</param>
        public bool Log(string message, LogType type)
        {
            try
            {
                switch (type)
                {
                    case LogType.DEBUG: log.Debug(message); break;
                    case LogType.ERROR: log.Error(message); break;
                    case LogType.FATAL: log.Fatal(message); break;
                    case LogType.INFORMATION: log.Info(message); break;
                    case LogType.WARNING: log.Warn(message); break;
                }

                return true;
            }

            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///     Overload using default type "debug".
        /// </summary>
        /// 
        /// <param name="message">Message</param>
        public void Log(string message)
        {
            Log(message, LogType.DEBUG);
        }

        # endregion
    }
}